package com.my12306.common.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.my12306.common.model.vo.PageVo;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.List;

/**
 * 分页工具类
 *
 * @Author:苏牧夕
 * @Date:2020/5/13 15:01
 * @Version 1.0
 */
@Component
public class PageUtils implements Serializable {
    private static PageUtils pageUtils = new PageUtils();
    /**
     * 总数据量
     */
    private int totalCount;
    /**
     * 每页页面大小
     */
    private int pageSize;
    /**
     * 当前页面
     */
    private int currPage;
    /**
     * 数据集
     */
    private List<?> list;
    /**
     * 总页数： 总数据量/页面大小 向上取整
     */
    private int totalPage;

    public PageUtils() {
    }

    public PageUtils(Integer totalCount, PageVo pageVo, List<?> data) {
        if (totalCount != null) {
            this.totalCount = totalCount;
        } else {
            this.totalCount = 0;
        }
        if (pageVo != null) {
            this.pageSize = pageVo.getLimit() == null ? 10 : pageVo.getLimit();
            this.currPage = pageVo.getPage() == null ? 1 : pageVo.getPage();
        } else {
            this.pageSize = 10;
            this.currPage = 1;
        }
        this.list = data;
    }

    /**
     * @param totalCount 总页数
     * @param pageSize   页面大小
     * @param currPage   当前页
     * @param list       数据集
     */
    public PageUtils(int totalCount, int pageSize, int currPage, List<?> list) {
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.currPage = currPage;
        this.list = list;
        this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
    }

    /**
     * 分页
     */
    public PageUtils(IPage<?> page) {
        this.list = page.getRecords();
        this.totalCount = (int) page.getTotal();
        this.pageSize = (int) page.getSize();
        this.currPage = (int) page.getCurrent();
        this.totalPage = (int) page.getPages();
    }

    public static PageUtils build(PageVo pageVo, List<?> dataList, Integer dataCount) {
        if (dataCount != null) {
            pageUtils.setTotalCount(dataCount);
        } else {
            pageUtils.setTotalCount(0);
        }
        if (pageVo != null) {
            pageUtils.setPageSize(pageVo.getLimit() == null ? 10 : pageVo.getLimit());
            pageUtils.setCurrPage(pageVo.getPage() == null ? 1 : pageVo.getPage());
        } else {
            pageUtils.setPageSize(10);
            pageUtils.setPageSize(1);
        }
        pageUtils.setList(dataList);
        return pageUtils;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getCurrPage() {
        return currPage;
    }

    public void setCurrPage(int currPage) {
        this.currPage = currPage;
    }

    public List<?> getList() {
        return list;
    }

    public void setList(List<?> list) {
        this.list = list;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
}
